#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTcpSocket>
#include <QFile>
#include <QTimer>
#include <QElapsedTimer>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_connectButton_clicked();
    void on_disconnectButton_clicked();
    void on_startReceiveButton_clicked();
    void on_browseButton_clicked(); // 新增浏览按钮槽函数

    void onSocketConnected();
    void onSocketDisconnected();
    void onSocketReadyRead();
    void onSocketError(QAbstractSocket::SocketError error);

//    void updateStats();
    void flushWriteBuffer();

private:
    Ui::MainWindow *ui;

    // 网络相关
    QTcpSocket *m_socket;
    QString m_serverIP;
    int m_port;

    // 文件相关
    QFile *m_dataFile;
    QByteArray m_writeBuffer;

    // 统计相关
    QTimer *m_statsTimer;
    QElapsedTimer m_speedTimer;
    qint64 m_totalBytesReceived;
    qint64 m_lastBytesReceived;
    int m_statCount;

    bool m_isConnected;
    bool m_isReceiving;

    void appendLog(const QString &message);
    void resetStats();
    bool initializeFile(const QString &filePath);
    void enableControls(bool connected, bool receiving);
};
#endif // MAINWINDOW_H
